import utils from "../../utils/index";
import { itemModel, userModel } from "../../apis/index";

const app = getApp()
Page({
  data: {
    list:[],
    rent_id:'',
    share:{}
  },
  onLoad: function (options) {
    let {rent_id,list} = app.globalData.rent_data
    this.data.rent_id = rent_id
    let arr = []
    list.forEach(el => {
      let temp = {
        id:el.id,
        name:el.name,
        default:el.price,
        price:''
      }
      arr.push(temp)
    })
    this.setData({
      list:arr
    })
  },
  onReady: function () {

  },
  onShow: function () {
    userModel.getShareData().then(res=>{
      this.data.share = res
    })
  },
  onShareAppMessage: function (e) {
    let uid = utils.getStorageSync('uid')
    return {
      title:this.data.share.shareTitle,
      path:`pages/procurement/index?rent_id=${this.data.rent_id}&&user_id=${uid}`,
      imageUrl:this.data.share.shareUrl
    }
  },
  bindInput(e){
    let index = e.currentTarget.dataset.index
    let value = e.detail.value.trim()
    let list = this.data.list
    let item = list[index]
    let reg =  /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
    if(!value||reg.test(value)){
      item.price = value
      this.setData({
        list:list
      })
    }else{
      this.setData({
        list:this.data.list
      })
    }
  },
  saveInfo(e){
    let {list} = this.data
    let index = e.currentTarget.dataset.index
    let item = list[index]
    if(!item.price){
      utils.showToast('价格不能设置为空','none')
      return false
    }
    let param = {
      lid:this.data.rent_id,
      pid:item.id,
      price:item.price
    }
    itemModel.fixLeasePrice(param).then(res=>{
    }).catch(()=>{
      utils.hideAll()
      utils.showToast('价格修改失败','none')
    })
  }
})